#include "pwm.h"
#define TIM_RELOAD_VALUE_US(x,prescale) (((48 / prescale) * x) - 1)
#define TIM_RELOAD_VALUE_MS(x,prescale) (((48000 / prescale) * x) -1)
void PWM_Config(void)
{
    TM_OutputInitTypeDef TM_OutputInitStruct;
    TM_OutputInitStruct.AsymmetricCompare = 0;
    TM_OutputInitStruct.Channel = TM_CH_1;
    TM_OutputInitStruct.OutputMode = TM_OM_PWM1;
    TM_OutputInitStruct.Compare = TIM_RELOAD_VALUE_MS(60,480);
    TM_OutputInitStruct.Control = TM_CHCTL_ENABLE;
    TM_OutputInitStruct.ControlN = TM_CHCTL_DISABLE;
    TM_OutputInitStruct.IdleState = MCTM_OIS_LOW;
    TM_OutputInitStruct.IdleStateN = MCTM_OIS_HIGH;
    TM_OutputInit(HT_GPTM0, &TM_OutputInitStruct);
    HT_GPTM0->CH0CCR = TIM_RELOAD_VALUE_MS(40,480);
    HT_GPTM0->CH0OCFR |= 0x00000010; //Preload CCR Enable
    HT_GPTM0->CH0OCFR |= 0x00000006; //PWM1 Mode
    HT_GPTM0->CHCTR   |= 0x00000001; //CH0 Output Enable
    HT_GPTM0->CHPOLR  |= 0x00000000; //High level is valid
    AFIO_GPxConfig(GPIO_PC, GPIO_PIN_4, AFIO_FUN_GPTM0);
    AFIO_GPxConfig(GPIO_PC, GPIO_PIN_5, AFIO_FUN_GPTM0);
}
